/*-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License.You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
	
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------*/

package fever.structures.colors 
{
	import fever.structures.Cloneable;	import fever.structures.IColor;	import fever.utils.ColorSpace;	import fever.utils.Stringifier;	
	/**
	 * Defines color using RGB componants
	 * 
	 * @see fever.structures.IColor
	 * @see fever.utils.ColorSpace
	 * 
	 * @author Romain Ecarnot
	 */
	public class RGB implements IColor, Cloneable
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------

		private var _red : int;
		private var _green : int;
		private var _blue : int;

		
		//--------------------------------------------------------------------
		// Public properties
		//--------------------------------------------------------------------
		
		/** Red value. */
		public function get red() : int 
		{ 
			return _red; 
		}

		/** @private */
		public function set red( n : int ) : void 
		{ 
			_setRed( n ); 
		}	

		
		/** Green value. */
		public function get green() : int 
		{ 
			return _green; 
		}

		/** @private */
		public function set green( n : int ) : void 
		{ 
			_setGreen( n ); 
		}	

		/** Blue value. */
		public function get blue() : int 
		{ 
			return _blue; 
		}

		/** @private */
		public function set blue( n : int ) : void 
		{ 
			_setBlue( n ); 
		}	

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 * 
		 * @param r Red value
		 * @param g Green value
		 * @param b Blue value
		 */
		public function RGB( r : int, g : int, b : int )
		{
			_setRed( r );
			_setGreen( g );
			_setBlue( b );
		}

		/** 
		 * Returns color value in Number format.
		 */
		public function getColor() : uint
		{
			return ColorSpace.rgbToHexa( this );
		}

		/**
		 * Return color value in hexa String format.
		 */ 
		public function getString() : String
		{
			return ColorSpace.rgbToString( this );
		}

		/**
		 * Returns a clone instance.
		 * 
		 * @return Cloneable
		 */
		public function clone() : Cloneable
		{
			return new RGB( _red, _green, _blue );
		}

		/**
		 * Returns string representation.
		 */
		public function toString() : String
		{
			return 	Stringifier.parse( this, "RGB [" + _red + "." + _green + "." + _blue + "]" );
		}

		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
		
		private function _setRed( n : int ) : void
		{
			if( n >= 0 && n <= 255 ) _red = n;
			else _red = 0;	
		}

		private function _setGreen( n : int ) : void
		{
			if( n >= 0 && n <= 255 ) _green = n;
			else _green = 0;	
		}
		
		private function _setBlue( n : int ) : void
		{
			if( n >= 0 && n <= 255 ) _blue = n;
			else _blue = 0;	
		}
	}
}
